﻿namespace MediatorPattern
{
    /*
    // 抽象牌友类
    public abstract class AbstractCardPartnet 
    {
        public int MoneyCount { get; set; }
        public AbstractCardPartnet() 
        {
            MoneyCount = 0;
        }
        public abstract void ChangeCount(int count, AbstractCardPartnet other);
    }

    // 牌友A类
    public class ParterA : AbstractCardPartnet 
    {
        public override void ChangeCount(int count, AbstractCardPartnet other)
        {
            this.MoneyCount += count;
            other.MoneyCount -= count;
        }
    }

    // 牌友B类
    public class ParterB : AbstractCardPartnet 
    {
        public override void ChangeCount(int count, AbstractCardPartnet other)
        {
            this.MoneyCount += count;
            other.MoneyCount -= count;
        }
    }

    public class Program 
    {
        // A,B两个人打牌
        static void Main(string[] args) 
        {
            AbstractCardPartnet A = new ParterA();
            A.MoneyCount = 20;

            AbstractCardPartnet B = new ParterB();
            B.MoneyCount = 20;

            // A赢了则B的钱就减少
            A.ChangeCount(5, B);
            Console.WriteLine("A 现在的钱是：{0}", A.MoneyCount); // 应该是25
            Console.WriteLine("B 现在的钱是：{0}", B.MoneyCount); // 应该是15

            // B赢了则A的钱就减少
            B.ChangeCount(10, A);
            Console.WriteLine("A 现在的钱是：{0}", A.MoneyCount); // 应该是15
            Console.WriteLine("B 现在的钱是：{0}", B.MoneyCount); // 应该是25
            Console.ReadLine();
        }
    }
    */

    // 抽象牌友类
    public abstract class AbstractCardPartner 
    {
        public int MoneryCount { get; set; }
        public AbstractCardPartner() 
        {
            MoneryCount = 0;
        }

        public abstract void ChangeCount(int count, AbstractMediator mediator);
    }

    // 牌友A类
    public class ParterA : AbstractCardPartner
    {
        // 依赖与抽象中介者对象
        public override void ChangeCount(int count, AbstractMediator mediator)
        {
            mediator.AWin(count);
        }
    }

    // 牌友B类
    public class ParterB : AbstractCardPartner 
    {
        // 依赖与抽象中介者对象
        public override void ChangeCount(int count, AbstractMediator mediator)
        {
            mediator.BWin(count);
        }
    }

    // 抽象中介者类
    public abstract class AbstractMediator 
    {
        protected AbstractCardPartner A;
        protected AbstractCardPartner B;
        public AbstractMediator(AbstractCardPartner a, AbstractCardPartner b) 
        {
            A = a;
            B = b;
        }
        public abstract void AWin(int count);
        public abstract void BWin(int count);
    }

    // 具体中介者类
    public class MediatorPater : AbstractMediator 
    {
        public MediatorPater(AbstractCardPartner a, AbstractCardPartner b) 
            : base(a, b) 
        {
            
        }

        public override void AWin(int count)
        {
            A.MoneryCount += count;
            B.MoneryCount -= count;
        }

        public override void BWin(int count)
        {
            B.MoneryCount += count;
            A.MoneryCount -= count;
        }
    }

    class Program 
    {
        static void Main(string[] args) 
        {
            AbstractCardPartner A = new ParterA();
            AbstractCardPartner B = new ParterB();

            // 初始线
            A.MoneryCount = 20;
            B.MoneryCount = 20;

            AbstractMediator mediator = new MediatorPater(A, B);

            // A赢了
            A.ChangeCount(5, mediator);
            Console.WriteLine("A 现在的钱是：{0}", A.MoneryCount); // 应该是25
            Console.WriteLine("B 现在的钱是：{0}", B.MoneryCount); // 应该是15

            // B赢了
            B.ChangeCount(10, mediator);
            Console.WriteLine("A 现在的钱是：{0}", A.MoneryCount); // 应该是15
            Console.WriteLine("B 现在的钱是：{0}", B.MoneryCount); // 应该是25

            Console.Read();
        }
    }
}

